package com.lib.controller.customer;

import com.lib.application.CustomerService;
import com.lib.application.InstallmentRequestService;
import com.lib.dto.InstallmentRequestDTO;
import com.lib.dto.enums.CustomerErrorEnum;
import com.lib.model.Customer;
import com.lib.model.enums.InstallmentStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by duqingxiang on 17/5/24.
 */
@Controller
@RequestMapping("/customer")
public class CustomerWxController {

    @Autowired
    CustomerService customerService;
    @Autowired
    InstallmentRequestService installmentRequestService;


    @RequestMapping("/installment/info")
    public ModelAndView installmentInfo(@RequestParam String openId
                                   , @RequestParam Long agentId) {

        ModelAndView view = new ModelAndView();

        Customer customer = customerService.getCustomerByOpenId(openId);
        if (customer == null) {
            view.setViewName("wxShow/customer/customer_error");
            return view;
        }

        InstallmentRequestDTO requestDTO = installmentRequestService.getNormalRequest(customer,agentId);
        if (requestDTO.getError() < 0) {
            view.setViewName(CustomerErrorEnum.valuesOf(requestDTO.getError()).getTemplate());
            return view;
        }



        //跳转至第一次开始申请的页面
        //第一步完成了，跳转至第二步
        if (requestDTO.getStatus() == null || requestDTO.getStatus() == InstallmentStatusEnum.PREPAREED) {
            view.addObject("request",requestDTO);
            view.setViewName("wxShow/customer/customer_req");
        } else if (requestDTO.getStatus() == InstallmentStatusEnum.UNCHECKED
                || requestDTO.getStatus() == InstallmentStatusEnum.UNCONFIRMED
                || requestDTO.getStatus() == InstallmentStatusEnum.RETURNED) {
            //审核、提交、打回   都给租户返回正在审核的页面

            view.setViewName("wxShow/customer/customer_checked");
        } else {//剩下就是一种审核成功的页面呢

        }




        return view;
    }


}
